package com.yequan;

import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author yequan
 * @date 2021/05/07 18:38
 **/
public class SignDemo {


    public static void main(String[] args) {

//        String paramStr = "orderId=773167574601957376&phone=MTU1MDYxMzIyNzk=";
        String paramStr = null;
        String timestamp ="1620382975181";
        String signKey = "0c03TVOlzy8";
        String checkStr = "&timestamp=" + timestamp + "&sign_key=" + signKey;
        System.out.println(checkStr);
        System.out.println(md5hash(checkStr));


//        String errorParamStr = "orderId=773167574601957378&phone=MTU1MDYxMzIyNzk="+ "&timestamp=" + timestamp + "&sign_key=" + signKey;
//        String errorParamStr = "&timestamp=" + timestamp + "&sign_key=" + signKey;
//        System.out.println(errorParamStr);
//        System.out.println(md5hash(errorParamStr));

    }

    public static String md5hash(String s) {
        if (StringUtils.isEmpty(s)) {
            return "";
        }
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] hashBytes = md5.digest(s.getBytes());
        StringBuilder hash = new StringBuilder();
        for (byte hashByte : hashBytes) {
            int v = hashByte & 0xFF;
            if (v < 16) hash.append(0);
            hash.append(Integer.toString(v, 16));
        }
        return hash.toString();
    }

}
